package cn.wolfcode.crm.util;

import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.domain.Permission;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpSession;

public class UserContext {
    private static final String EMPLOYEE_IN_SESSION = "EMPLOYEE_IN_SESSION";
    private static final String PERMISSION_IN_SESSION = "PERMISSION_IN_SESSION";

    //这里是张岩

    //将employee放进session中
    public static void setCurrentEmployee(Employee employee) {
        getHttpSession().setAttribute(EMPLOYEE_IN_SESSION, employee);
    }

    //从session中获取employee
    public static Employee getCurrentEmployee() {
        return (Employee) getHttpSession().getAttribute(EMPLOYEE_IN_SESSION);
    }

    //将permission放进session中
    public static void setExpression(Permission permission) {
        getHttpSession().setAttribute(PERMISSION_IN_SESSION, permission);
    }

    //从session中获取expression
    public static Permission getExpression() {
        return (Permission) getHttpSession().getAttribute(PERMISSION_IN_SESSION);
    }

    public static HttpSession getHttpSession() {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        return requestAttributes.getRequest().getSession();
    }
}
